package com.neu.servlet.ww;

import com.neu.service.ww.PlayService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(name = "UpPlayServlet",urlPatterns = "/UpPlayServlet.do")
@MultipartConfig
public class UpPlayServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part part=request.getPart("p_img");
        //tomcat8才能用这个方法
//        String p_img=part.getSubmittedFileName();
        String fileName=part.getHeader("Content-Disposition");
        String p_img=fileName.substring(fileName.lastIndexOf("."),fileName.length()-1);
        if (".jpg".equals(p_img)||".gif".equals(p_img)){
            p_img=System.currentTimeMillis()+p_img;
            String path=request.getServletContext().getRealPath("/static/img/play");
            part.write(path+"/"+p_img);
            PlayService playService=new PlayService();
            int row=playService.doSave(p_img);
            if (row>0){
                request.getRequestDispatcher("/WEB-INF/views/mgr.jsp").forward(request,response);
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
